﻿using System.Windows.Forms;

namespace InstallPartner
{
    /// <summary>
    /// Provides the UI for adding a new profile.
    /// </summary>
    public partial class AddProfileDialog : Form
    {
        #region Private Attributes

        private readonly InstallHelper _installHelper;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AddProfileDialog"/> class.
        /// </summary>
        /// <param name="installHelper">An instance of <see cref="InstallHelper"/>.</param>
        public AddProfileDialog(InstallHelper installHelper)
        {
            InitializeComponent();

            _installHelper = installHelper;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the name of the profile.
        /// </summary>
        /// <value>The name of the profile.</value>
        public string ProfileName
        {
            get
            {
                return txtName.Text;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the TextChanged event of the txtName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ValueChanged(object sender, System.EventArgs e)
        {
            btnOk.Enabled = txtName.Text.Length > 0;
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, System.EventArgs e)
        {
            if (_installHelper.GetProfileId(txtName.Text) > 0)
            {
                // Do not allow duplicate names
                MessageBox.Show(
                    this,
                    string.Format("The profile '{0}' already exists.  Please enter a new name.", txtName.Text),
                    "Add Profile",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
            else if (txtName.Text.Contains(" "))
            {
                // Do not allow spaces in the profile name
                MessageBox.Show(
                    this,
                    "The profile name cannot contain spaces.",
                    "Add Profile",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        #endregion
    }
}
